﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Aletfa.LightManager
{
    public sealed class Unit
    {
        public Guid Id { get; private set; }

        public Dictionary<DamageSources, int> Resistances { get; set; }
        public Dictionary<DamageSources, int> Armors { get; set; }
        public Dictionary<DamageSources, int> Shields { get; set; }

        public string ArmorDescription { get; set; }

        public string Name { get; set; }
        public string PlayerName { get; set; }

        public List<WoundType> LifeStatus { get; set; }

        public int CurrentActions { get; set; }

        public string ImageFile { get; set; }

        public bool IsEnemy { get; set; }

        public List<string> CustomStatus { get; set; }

        public Unit()
        {
            this.Id = Guid.NewGuid();

            this.Resistances = new Dictionary<DamageSources, int>();
            this.Armors = new Dictionary<DamageSources, int>();
            this.Shields = new Dictionary<DamageSources, int>();
            this.LifeStatus = new List<WoundType>();
            this.CustomStatus = new List<string>();

            foreach (DamageSources school in Enum.GetValues(typeof(DamageSources)))
            {
                this.Resistances.Add(school, 0);
                this.Armors.Add(school, 0);
                this.Shields.Add(school, 0);
            }
            this.Resistances.Remove(DamageSources.Phisical);
            this.Resistances.Remove(DamageSources.Ethereal);
            this.Armors.Remove(DamageSources.Ethereal);
        }

        public void Initialize(int life)
        {
            this.LifeStatus = new List<WoundType>();
            for (int i = 0; i < life; i++)
                this.LifeStatus.Add(WoundType.Free);
            this.CurrentActions = 3;
        }

        public void Reset()
        {
            this.CurrentActions = 3;
            for (int i = 0; i < this.LifeStatus.Count; i++)
                if (this.LifeStatus[i] != WoundType.Aggravated)
                    this.LifeStatus[i] = WoundType.Free;
        }

        public int LifeCount
        {
            get { return (from WoundType l in this.LifeStatus where l == WoundType.Free select l).Count(); }
        }

        public override string ToString()
        {
            return (string.IsNullOrEmpty(this.PlayerName)) ? this.Name : string.Format("{0} ({1})", this.Name, this.PlayerName);
        }
    }
}
